﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using EntityManage;
using BusinessFacade;

public partial class Modules_Personal_PerfApplyAppAdd : System.Web.UI.Page
{
    IPerf_Flow_TraceHandler traceHandler = new Perf_Flow_TraceManage();
    IPerf_FlowHandler flowHandler = new Perf_FlowManage();
    IPerf_Flow_StepsHandler stepsHandler = new Perf_Flow_StepsManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    IPerf_Flow_TraceDtlHandler traceDtlHandler = new Perf_Flow_TraceDtlManage();
    IPerf_Flow_TraceAttachHandler traceAttachHandler = new Perf_Flow_TraceAttachManage();

    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();
    IPerf_Personal_EvaluateHandler evaHandler = new Perf_Personal_EvaluateManage();

    IPerf_Personal_ModTargetHandler modTargetHandler = new Perf_Personal_ModTargetManage();
    IPerf_Personal_ModTargetDtlHandler modTargetDtlHandler = new Perf_Personal_ModTargetDtlManage();
    IPerf_Personal_FinalAssignHandler finalAssignHandler = new Perf_Personal_FinalAssignManage();
    IPerf_Personal_FinalAssignDtlHandler faDtlHandler = new Perf_Personal_FinalAssignDtlManage();
    IPerf_Personal_TargetHandler targetHandler = new Perf_Personal_TargetManage();
    IPerf_Personal_TargetOwnerHandler toHandler = new Perf_Personal_TargetOwnerManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            if (Session["UserPurviews"] != null)
            {
                string strPur = Session["UserPurviews"].ToString();
                if (strPur.Contains("303015"))
                {
                    divAdd.Style.Add("display", "block");
                }
                else
                {
                    divAdd.Style.Add("display", "none");
                }
            }
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("303030"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] ttIds = Request["ttIds"].Split(',');
        int userId = int.Parse(Session["CurrentUserID"].ToString());
        Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(userId);

        HttpFileCollection files = HttpContext.Current.Request.Files;

        // 调用公共方法，处理流程
        bool isLastStep = false;
        int isPast = int.Parse(rblIsPass.SelectedValue);
        FlowTrace.ProcessFlowTraceStep(ttIds, txtComment.Text, isPast, userQuery, files,ref isLastStep);
        
        foreach (string tmpId in ttIds)
        {
            int traceId = int.Parse(tmpId);
            Perf_Flow_TraceQuery traceQuery = traceHandler.GetPerf_Flow_TraceQuery(traceId);
            Perf_Personal_ApprovalQuery approvalQuery = approvalHandler.GetPerf_Personal_ApprovalQuery(traceQuery.Business_ID);

            // 如果审批不通过，或者到了流程的最后一步，更新评分表和呈批表的状态
            if (isPast != 0 || isLastStep == true)
            {
                approvalQuery.Status = isPast == 0 ? (int)Enums.PersonalApprovalStatus.Passed : (int)Enums.PersonalApprovalStatus.NotPassed;
                approvalHandler.UpdatePerf_Personal_Approval(approvalQuery);

                Perf_Personal_EvaluateQuery[] evaList = evaHandler.GetPerf_Personal_EvaluateQueryList(" Approval_ID = @Approval_ID", new SqlParameter[] { new SqlParameter("@Approval_ID", approvalQuery.ID) });
                if (evaList != null && evaList.Length > 0)
                {
                    foreach (Perf_Personal_EvaluateQuery evaQuery in evaList)
                    {
                        evaQuery.Status = isPast == 0 ? (int)Enums.PersonalEvaluateStatus.Passed : (int)Enums.PersonalEvaluateStatus.NotPassed;
                        evaHandler.UpdatePerf_Personal_Evaluate(evaQuery);
                    }
                }
            }

            //所长加分
            if (isPast == 0)
            {
                if (Session["UserPurviews"] != null)
                {
                    Perf_Personal_ApprovalQuery approvalQuery2 = approvalHandler.GetPerf_Personal_ApprovalQuery(traceQuery.Business_ID);

                    string strPur = Session["UserPurviews"].ToString();
                    if (strPur.Contains("303015"))
                    {
                        float score = 0;
                        float.TryParse(txtPlusScore.Text, out score);
                        approvalQuery2.Plus_Score = score;
                        approvalQuery2.Plus_Reason = txtPlusReason.Text.Trim();
                        approvalHandler.UpdatePerf_Personal_Approval(approvalQuery2);
                    }
                }
            }

            // 如果流程最后一步，而且审批通过
            if (isLastStep == true && isPast == 0)
            {
                //// 将评分表和呈批表状态改为公示
                //approvalQuery.Status = (int)Enums.PersonalApprovalStatus.Notice;
                //approvalHandler.UpdatePerf_Personal_Approval(approvalQuery);

                //Perf_Personal_EvaluateQuery[] evaList = evaHandler.GetPerf_Personal_EvaluateQueryList(" Approval_ID = @Approval_ID", new SqlParameter[] { new SqlParameter("@Approval_ID", approvalQuery.ID) });
                //if (evaList != null && evaList.Length > 0)
                //{
                //    foreach (Perf_Personal_EvaluateQuery evaQuery in evaList)
                //    {
                //        evaQuery.Status = (int)Enums.PersonalEvaluateStatus.Notice;
                //        evaHandler.UpdatePerf_Personal_Evaluate(evaQuery);
                //    }
                //}

                // TODO
            }
        }

        Jscript.ReturnAndClose(this.Page, "true");
    }

}
